package com.moleq.mgnetwork;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.Handler;
import android.os.Message;

import com.moleq.mgnetwork.MultiCommunication.CommunicationCallback;

/*
 * 
 * mutil conncet model
 */
public class ConnectHandler extends Thread {

	protected static final int IP_UNREGISTER = 0;

	protected static final int IP_REGISTER = 1;

	protected static final int IP_CONNECTED = 2;

	private static final int SHOW_COMMUNICATION_MSG = 3;

	private ServerSocket sSocket = null;
	private Socket clientSocket = null;
	private InetSocketAddress isa;
	private CommunicationCallback mCommunicationCallback = null;

	public ConnectHandler(CommunicationCallback callback) {
		this.mCommunicationCallback = callback;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();

		try {
			sSocket = new ServerSocket(Constant.CMM_PORT);
			System.out.println("connect handler socket started...");
			while (!sSocket.isClosed() && null != sSocket) {
				Socket socket = sSocket.accept();
				socket.setSoTimeout(5000);
				new ProcessReceivePackage(socket).start();
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	private class ProcessReceivePackage extends Thread {

		private Socket socket = null;

		public ProcessReceivePackage(Socket socket) {
			this.socket = socket;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			// super.run();

			try {

				if (!socket.isClosed())
					socket.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}

	}

	private final Handler communicationHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case IP_CONNECTED:
				if (mCommunicationCallback != null)
					mCommunicationCallback.ipAddressConnected(msg.obj.toString(), (Boolean) msg.obj);
				break;
			case IP_REGISTER:
				if (mCommunicationCallback != null)
					mCommunicationCallback.register(msg.obj.toString());
				break;
			case IP_UNREGISTER:
				if (mCommunicationCallback != null)
					mCommunicationCallback.unRegister(msg.obj.toString());
				break;
			case SHOW_COMMUNICATION_MSG:
				//
				if (mCommunicationCallback != null)
					mCommunicationCallback.showLogMessage(msg.obj.toString());
				break;

			default:
				break;
			}

		}
	};

	public void pingIp(String ipAddress) {
		try {
			clientSocket = new Socket();
			isa = new InetSocketAddress(ipAddress, Constant.CMM_PORT);
			clientSocket.connect(isa, 5000);

			if (clientSocket.isConnected()) {

				Message msg = new Message();
				msg.what = MultiCommunication.IP_REGISTER;
				msg.obj = ipAddress;
				communicationHandler.sendMessage(msg);

				sleep(1000);// sleep 1秒钟，等待对方处理完
			}

		} catch (Exception e) {
			// e.printStackTrace();
			// showMsg("Connecthandler.pingIp  error ->" + e.getMessage());
			Message msg = new Message();
			msg.what = MultiCommunication.IP_UNREGISTER;
			msg.obj = ipAddress;
			communicationHandler.sendMessage(msg);
		} finally {
			try {
				if (clientSocket != null) {
					clientSocket.close();
				}
			} catch (Exception e) {
				// e.printStackTrace();
				// showMsg("Connecthandler.pingIp  error ->" + e.getMessage());
				Message msg = new Message();
				msg.what = MultiCommunication.IP_UNREGISTER;
				msg.obj = ipAddress;
				communicationHandler.sendMessage(msg);
			}
		}
	}

	public void release() {
		try {
			System.out.println("conncet handler socket closed ...");
			if (null != sSocket)
				sSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
